//
//  AppDelegate.m
//  aMoment
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "AppDelegate.h"
#import "AllHeaderFiles.h"

@interface AppDelegate ()
@property(nonatomic, retain)UIImageView *launchImageView;//引导页
@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [_launchImageView release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
    [_window release];
    
    //设置时间栏文字颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    
    //判断当前是否是夜间模式
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"night"]) {
        _window.alpha = .5;
    } else {
        _window.alpha = 1;
    }
    
    //创建抽屉
    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    DDMenuController *menuController = [[DDMenuController alloc] initWithRootViewController:nav];
    LeftViewController *leftVC = [[LeftViewController alloc] init];//左侧菜单
    menuController.leftViewController = leftVC;
    _window.rootViewController = menuController;
    [vc release];
//    [nav release];//不能release
//    [leftVC release];//不能release
    [menuController release];
    
    //判断是不是第一次启动
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"first"]) {//不是第一次启动
        //启动页
        self.launchImageView = [[UIImageView alloc] initWithFrame:_window.frame];
        _launchImageView.image = [UIImage imageNamed:@"3"];
        [_window addSubview:_launchImageView];
        [_launchImageView release];
        
        //放大动画
        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        basicAnimation.fromValue = [NSNumber numberWithDouble:1];//原始比例
        basicAnimation.toValue = [NSNumber numberWithDouble:1.3];//放大倍数
        [basicAnimation setDuration:3];
        [_launchImageView.layer addAnimation:basicAnimation forKey:@"launch"];
        
        //移除启动页
        [UIView animateWithDuration:3 animations:^{
            _launchImageView.alpha = 0;
        }];
        [UIView animateWithDuration:3 animations:^{
            _launchImageView.alpha = 0;
        } completion:^(BOOL finished) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [_launchImageView removeFromSuperview];
            });
        }];
    }
    else {//第一次启动
        //引导页
        GuideView *guideView = [[GuideView alloc] initWithFrame:_window.frame];
        [_window addSubview:guideView];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"first"];
    }
    
    //设置友盟key
    [UMSocialData setAppKey:@"562490db67e58ef75f001384"];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
